package com.pdsu.graduation_design.views;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.pdsu.graduation_design.MainActivity;
import com.pdsu.graduation_design.R;
import com.pdsu.graduation_design.adapter.NewsAgricultureMessageAdapter;
import com.pdsu.graduation_design.adapter.PreGoodsGridViewAdapter;
import com.pdsu.graduation_design.domain.Agriculture;
import com.pdsu.graduation_design.domain.Commodity;
import com.pdsu.graduation_design.listener.NewsAgricultureMessageListener;
import com.pdsu.graduation_design.utils.JsonUtil;
import com.pdsu.graduation_design.utils.Result;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class News_Fragment extends Fragment {
    private View currentView;
    private TextView bar_top_textview;
    private ListView agriculture_message_lv;
    //Data
    private  static List<Agriculture> agricultures;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        currentView=inflater.inflate(R.layout.fragment_news,null);

        /**
         * 获取数据
         */
        //数据
        init();
        operation();
        bind();
        return currentView;
    }
    private void operation() {

        bar_top_textview.setText("新闻");


    }

    private void init() {
        agricultures=new ArrayList<>();
        bar_top_textview = currentView.findViewById(R.id.bar_top_common_textview);
        agriculture_message_lv=currentView.findViewById(R.id.news_listview);

    }
    private void bind(){
        /**
         * 将AgricultureList数据传入Adapter
         */
        getData();
/**
 * 将AgricultureList数据传入Listener
 */



    }

    private void getData(){

        System.out.println("getdata");


        RequestParams requestParams=new RequestParams(MainActivity.URL+"/agriculture/getAllAgriculture");
        x.http().post(requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                System.out.println("--------------------");
                try {
                    Result r= JsonUtil.JsonToObj(result,Result.class);
                    //获取数据成功
                    if (r.getCode().equals("200")){
                        agricultures=JsonUtil.json2list(r.getData().toString(), Agriculture.class);
                        agriculture_message_lv.setAdapter(new NewsAgricultureMessageAdapter(agricultures,currentView.getContext()));
                        agriculture_message_lv.setOnItemClickListener(new NewsAgricultureMessageListener(currentView.getContext(),agricultures));


                    }else {
                        System.out.println("error");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });



    }

}
